﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using control.Biz;

namespace control
{
    public partial class FrmQLSach : Form
    {
        List<BizSach> sachs = new List<BizSach>();
        List<BizChuDe> chudes = new List<BizChuDe>();
        public FrmQLSach()
        {
            InitializeComponent();


        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmQLSach_Load(object sender, EventArgs e)
        {
            binding();
            chudes = BizChuDe.GetAll();
            BizChuDe tatca = new BizChuDe(0, "Tất cả");
            chudes.Add(tatca);
            comboBox1.DataSource = chudes;
            comboBox1.DisplayMember = "TenChuDe";
            comboBox1.ValueMember = "MaChuDe";
        }

        private void binding()
        {
            
            sachs = BizSach.GetAll();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = sachs;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            BizSach sach;
            foreach(DataGridViewRow row in dataGridView1.SelectedRows)
            {
                sach = sachs[row.Index];
                FrmSach frm = new FrmSach(sach);
                frm.ShowDialog() ;
            }
            binding();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Ban co muon xoa khong ?", "Xac Dinh", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                BizSach sach;
                foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                {
                    sach = sachs[row.Index];
                    sach.Delete();
                }
                binding();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            BizSach sach = new BizSach();
            sach.IdSach = -1;
            FrmSach frm = new FrmSach(sach);
            frm.ShowDialog();
            binding();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sach = textBox1.Text;
            int IDchude = Convert.ToInt32(comboBox1.SelectedValue.ToString());
            int giatu = 0;
            try { giatu = Convert.ToInt32(textBox2.Text); }
            catch{}
            int giaden = Int32.MaxValue;
            try { giaden = Convert.ToInt32(textBox3.Text); }
            catch { }
            bool dangban=checkBox1.Checked;
            sachs=BizSach.Find(sach, IDchude, giatu, giaden, dangban);
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = sachs;


        }

        private void button2_Click(object sender, EventArgs e)
        {
            binding();
        }


    }
}
